class TestBridge extends Test
{
    public constructor(sprite:DebugDraw)
    {
        super(sprite);
        this.createScene();
        console.log("test bridge")
    }

    private createScene():void
    {
        var ground:Box2D.Dynamics.b2Body = this.m_world.GetGroundBody();
        var i:number;
        var anchor:Box2D.Common.Math.b2Vec2 = new Box2D.Common.Math.b2Vec2();
        var body:Box2D.Dynamics.b2Body;

        //bridge
        {
            let sd:Box2D.Collision.Shapes.b2PolygonShape = new Box2D.Collision.Shapes.b2PolygonShape();
            var fixtureDef:Box2D.Dynamics.b2FixtureDef = new Box2D.Dynamics.b2FixtureDef();
            sd.SetAsBox(24 / this.m_physScale, 5 / this.m_physScale);
            fixtureDef.shape = sd;
            fixtureDef.density = 20;
            fixtureDef.friction = 0.2;

            let bd:Box2D.Dynamics.b2BodyDef = new Box2D.Dynamics.b2BodyDef();
            bd.type = Box2D.Dynamics.b2Body.b2_dynamicBody;

            let jd:Box2D.Dynamics.Joints.b2RevoluteJointDef = new Box2D.Dynamics.Joints.b2RevoluteJointDef();
            const numPlanks:number = 10;
            jd.lowerAngle = -15 / (180 / Math.PI);
            jd.upperAngle =  15 / (180 / Math.PI);
            jd.enableLimit = true;

            let prevBody:Box2D.Dynamics.b2Body = ground;
            for(i = 0; i < numPlanks; i++)
            {
                bd.position.Set((100 + 22 + 44 * i) / this.m_physScale, 250 / this.m_physScale);
                body = this.m_world.CreateBody(bd);
                body.CreateFixture(fixtureDef);

                anchor.Set((100 + 44 * i) / this.m_physScale, 250 / this.m_physScale);
                jd.Initialize(prevBody, body, anchor);
                this.m_world.CreateJoint(jd);
                prevBody = body;
            }

            anchor.Set((100 + 44 * numPlanks) / this.m_physScale, 250 / this.m_physScale);
            jd.Initialize(prevBody, ground, anchor);
            this.m_world.CreateJoint(jd);
        }

        //spawn in a bunch of crap

        for(i = 0; i < 5; i++)
        {
            let bodyDef:Box2D.Dynamics.b2BodyDef = new Box2D.Dynamics.b2BodyDef();
            bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
            let boxShape:Box2D.Collision.Shapes.b2PolygonShape = new Box2D.Collision.Shapes.b2PolygonShape();
            fixtureDef.shape =boxShape;
            fixtureDef.density = 1.0;

            fixtureDef.friction = 0.3;
            fixtureDef.restitution = 0.1;
            boxShape.SetAsBox((Math.random() * 5 + 10) / this.m_physScale, (Math.random() * 5 + 10) / this.m_physScale);
            bodyDef.position.Set((Math.random() * 400 + 120) / this.m_physScale, (Math.random() * 150 + 50) / this.m_physScale);
            bodyDef.angle = Math.random() * Math.PI;
            body = this.m_world.CreateBody(bodyDef);
            body.CreateFixture(fixtureDef);
        }

        for(i = 0; i < 5; i++)
        {
            let bodyDefC:Box2D.Dynamics.b2BodyDef = new Box2D.Dynamics.b2BodyDef();
            bodyDefC.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
            let circShape:Box2D.Collision.Shapes.b2CircleShape = new Box2D.Collision.Shapes.b2CircleShape((Math.random() * 5 + 10)/ this.m_physScale);
            fixtureDef.shape =circShape;
            fixtureDef.density = 1.0;

            fixtureDef.friction = 0.3;
            fixtureDef.restitution = 0.1;
            //boxShape.SetAsBox((Math.random() * 5 + 10) / this.m_physScale, (Math.random() * 5 + 10) / this.m_physScale);
            bodyDefC.position.Set((Math.random() * 400 + 120) / this.m_physScale, (Math.random() * 150 + 50) / this.m_physScale);
            bodyDefC.angle = Math.random() * Math.PI;
            body = this.m_world.CreateBody(bodyDefC);
            body.CreateFixture(fixtureDef);
        }

        var j:number;
        for(i = 0; i < 15; i++)
        {
            let bodyDefP:Box2D.Dynamics.b2BodyDef = new Box2D.Dynamics.b2BodyDef();
            bodyDefP.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
            let polyShape:Box2D.Collision.Shapes.b2PolygonShape = new Box2D.Collision.Shapes.b2PolygonShape();
            let vertices:Box2D.Common.Math.b2Vec2[] = [];
            let vertexCount:number;
            if(Math.random() > 0.66)
            {
                vertexCount = 4;
                for(j = 0; j< vertexCount; j++)
                {
                    vertices[j] = new Box2D.Common.Math.b2Vec2();
                }
                vertices[0].Set((-10 - Math.random()*10) / this.m_physScale, (10 + Math.random() * 10) / this.m_physScale);
                vertices[1].Set((-5 - Math.random()*10) / this.m_physScale, (-10 - Math.random() * 10) / this.m_physScale);
                vertices[2].Set((5 + Math.random()*10) / this.m_physScale, (-10 - Math.random() * 10) / this.m_physScale);
                vertices[3].Set((10 + Math.random()*10) / this.m_physScale, (10 + Math.random() * 10) / this.m_physScale);
            }
            else if(Math.random() > 0.5)
            {
                vertexCount = 5;
                for(j = 0; j< vertexCount; j++)
                {
                    vertices[j] = new Box2D.Common.Math.b2Vec2();
                }
                vertices[0].Set(0, (10 + Math.random() * 10) / this.m_physScale);
                vertices[2].Set((-5 - Math.random()*10) / this.m_physScale, (-10 - Math.random() * 10) / this.m_physScale);
                vertices[3].Set((5 + Math.random()*10) / this.m_physScale, (-10 - Math.random() * 10) / this.m_physScale);
                vertices[1].Set(vertices[0].x + vertices[2].x, vertices[0].y + vertices[2].y);
                vertices[1].Multiply(Math.random() / 2 + 0.8);
                vertices[4].Set(vertices[3].x + vertices[0].x, vertices[3].y + vertices[0].y);
                vertices[4].Multiply(Math.random() / 2 + 0.8);
            }
            else
            {
                vertexCount = 3;
                for(j = 0; j< vertexCount; j++)
                {
                    vertices[j] = new Box2D.Common.Math.b2Vec2();
                }
                vertices[0].Set(0, (10 + Math.random() * 10) / this.m_physScale);
                vertices[1].Set((-5 - Math.random()*10) / this.m_physScale, (-10 - Math.random() * 10) / this.m_physScale);
                vertices[2].Set((5 + Math.random()*10) / this.m_physScale, (-10 - Math.random() * 10) / this.m_physScale);
                
            }
            polyShape.SetAsArray(vertices,vertexCount);
            fixtureDef.shape = polyShape;
            fixtureDef.density = 1.0;
            fixtureDef.friction = 0.3;
            fixtureDef.restitution = 0.1;
            bodyDefP.position.Set((Math.random() * 400 + 120) / this.m_physScale, (Math.random() * 150 + 50)/ this.m_physScale);
            bodyDefP.angle = Math.random() * Math.PI;
            body = this.m_world.CreateBody(bodyDefP);
            body.CreateFixture(fixtureDef);
        }
    }
}